package com.edu.oma.protocol.controller;

import com.edu.oma.protocol.common.BaseResult;
import com.edu.oma.protocol.common.BusinessException;
import com.edu.oma.protocol.common.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * controller 增强器
 * 用于 1.异常处理   2.初始化数据绑定器  3.绑定到Model中
 */
@RestControllerAdvice
public class RestControllerAdvicer {

    private Logger logger = LoggerFactory.getLogger(RestControllerAdvicer.class);


    /**
     * 业务异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public BaseResult handleBusinessException(BusinessException e){
        return new BaseResult(e.getStatus(),e.getMsg());
    }

    /**
     * 未知异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public BaseResult handleException(Exception e){
        logger.error(">>>>>>>>>>>>>>>>>>>>>>>>未知异常,msg:"+e.getMessage()+"<<<<<<<<<<<<<<<<<<<<<<<<<<");
        logger.error("excep "+e);
        e.printStackTrace();
        return new BaseResult(ResultCode.UNKNOWN_ERR_CODE,ResultCode.UNKNOWN_ERR_MSG);
    }
}
